{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# You first need to download the wordnet following these commands \n",
    "# before importing it\n",
    "import nltk\n",
    "nltk.download('wordnet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# you will need to download the wordnet corpus from nltk using nltk.download()\n",
    "from nltk.corpus import wordnet as wn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Various Synset Relationships\n",
    "Here we will look at what lemmas, hypernyms, hyponyms, meronyms and holonyms look like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All the available Synsets for  car\n",
      "\t [Synset('car.n.01'), Synset('car.n.02'), Synset('car.n.03'), Synset('car.n.04'), Synset('cable_car.n.01')] \n",
      "\n",
      "Example definitions of available Synsets ...\n",
      "\t car.n.01 :  a motor vehicle with four wheels; usually propelled by an internal combustion engine\n",
      "\t car.n.02 :  a wheeled vehicle adapted to the rails of railroad\n",
      "\t car.n.03 :  the compartment that is suspended from an airship and that carries personnel and the cargo and the power plant\n",
      "\n",
      "\n",
      "Example lemmas for the Synset  car.n.03\n",
      "\t ['car', 'auto', 'automobile'] \n",
      "\n",
      "Hypernyms of the Synset  car.n.01\n",
      "\t motor_vehicle.n.01 \n",
      "\n",
      "Hyponyms of the Synset  car.n.01\n",
      "\t ['ambulance.n.01', 'beach_wagon.n.01', 'bus.n.04'] \n",
      "\n",
      "Holonyms (Part) of the Synset  car.n.03\n",
      "\t ['airship.n.01'] \n",
      "\n",
      "Meronyms (Part) of the Synset  car.n.01\n",
      "\t ['accelerator.n.01', 'air_bag.n.01', 'auto_accessory.n.01'] \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# shows all the available synsets\n",
    "word = 'car'\n",
    "car_syns = wn.synsets(word)\n",
    "print('All the available Synsets for ',word)\n",
    "print('\\t',car_syns,'\\n')\n",
    "\n",
    "# The definition of the first two synsets\n",
    "syns_defs = [car_syns[i].definition() for i in range(len(car_syns))]\n",
    "print('Example definitions of available Synsets ...')\n",
    "for i in range(3):\n",
    "    print('\\t',car_syns[i].name(),': ',syns_defs[i])\n",
    "print('\\n')\n",
    "\n",
    "# Get the lemmas for the first Synset\n",
    "print('Example lemmas for the Synset ',car_syns[i].name())\n",
    "car_lemmas = car_syns[0].lemmas()[:3]\n",
    "print('\\t',[lemma.name() for lemma in car_lemmas],'\\n')\n",
    "\n",
    "# Let us get hypernyms for a Synset (general superclass)\n",
    "syn = car_syns[0]\n",
    "print('Hypernyms of the Synset ',syn.name())\n",
    "print('\\t',syn.hypernyms()[0].name(),'\\n')\n",
    "\n",
    "# Let us get hyponyms for a Synset (specific subclass)\n",
    "syn = car_syns[0]\n",
    "print('Hyponyms of the Synset ',syn.name())\n",
    "print('\\t',[hypo.name() for hypo in syn.hyponyms()[:3]],'\\n')\n",
    "\n",
    "# Let us get part-holonyms for a Synset (specific subclass)\n",
    "# also there is another holonym category called \"substance-holonyms\"\n",
    "syn = car_syns[2]\n",
    "print('Holonyms (Part) of the Synset ',syn.name())\n",
    "print('\\t',[holo.name() for holo in syn.part_holonyms()],'\\n')\n",
    "\n",
    "# Let us get meronyms for a Synset (specific subclass)\n",
    "# also there is another meronym category called \"substance-meronyms\"\n",
    "syn = car_syns[0]\n",
    "print('Meronyms (Part) of the Synset ',syn.name())\n",
    "print('\\t',[mero.name() for mero in syn.part_meronyms()[:3]],'\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Similarity between Synsets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Word Similarity (car)<->(lorry):  0.6956521739130435\n",
      "Word Similarity (car)<->(tree):  0.38095238095238093\n"
     ]
    }
   ],
   "source": [
    "word1, word2, word3 = 'car','lorry','tree'\n",
    "w1_syns, w2_syns, w3_syns = wn.synsets(word1), wn.synsets(word2), wn.synsets(word3)\n",
    "\n",
    "print('Word Similarity (%s)<->(%s): '%(word1,word2),wn.wup_similarity(w1_syns[0], w2_syns[0]))\n",
    "print('Word Similarity (%s)<->(%s): '%(word1,word3),wn.wup_similarity(w1_syns[0], w3_syns[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
